package zunge.lbe1;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

import zunge.tools.DbConn;

public class M_LBE1 {
	
	static String dataFolder = "/usr0/home/bliu1/BinWork/Workspace/NLBE/data/";
	static String libFolder = "/usr0/home/bliu1/BinWork/Workspace/NLBE/library/";
	
	public static void main(String args[]){
		//prediction();
		clustering();
	}
	static void prediction(){
		try{
			//flow1();
			
			//flow2gen();
			//flow2run();
			//Thread.sleep(1000*60);
			//flow2end();
			
			//flow3gen();
			//Need manually run flow3.sh
			
			flow3train();
			Thread.sleep(1000*60);
			flow3end();
			
		}catch(Exception e){e.printStackTrace();}
	}
	static void clustering(){
		try{
			//clusteringfolds();
			//clusteringfoldsTrain();
			clusteringfoldsCollect();
		}catch(Exception e){e.printStackTrace();}
	}
	
	

	
	/* Prediction */
	
	/**
	 * Flow 1: Data Generation
	 * DataInput
	 * List<DataFold>
	 */
	public static void flow1() throws SQLException, IOException{
		System.out.println("flow1");
		DbConn conn = new DbConn("127.0.0.1", "lbe_stream","root","sergeypk428571");
		Data dataInput = new Data(dataFolder, true, conn);
		DataFold.getDataFolds(dataInput, true);
		conn.close();
	}
	
	/**
	 * Flow 2: Direct classification
	 * @throws InterruptedException 
	 */
	public static void flow2gen() throws IOException, SQLException, InterruptedException{
		System.out.println("flow2");
		Data data = new Data(dataFolder, false, null);
		ScriptGen sg = new ScriptGen(libFolder);
			
		List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
		Flow2 flow2 = new Flow2(dataFolds);
		
		flow2.generate(sg);
		
	}
	public static void flow2run() throws Exception{
		System.out.println("flow2run");
		
		Data data = new Data(dataFolder, false, null);
		List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
		Flow2 flow2 = new Flow2(dataFolds);
		
		String cmd = "sh "+data.getScriptDir()+"flow2.sh &";
		flow2.run(cmd, data.getTempDir(), "flow2");
	}
	public static void flow2end() throws Exception{
		System.out.println("flow2end");
		Data data = new Data(dataFolder, false, null);
		List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
		Flow2 flow2 = new Flow2(dataFolds);
		flow2.collectResult();
	}
	
	
	/**
	 * Flow 3: Introducing collaborative info
	 */
	public static void flow3gen() throws IOException, SQLException{
		System.out.println("flow3gen");
		Data data = new Data(dataFolder, false, null);
		List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
		ScriptGen sg = new ScriptGen(libFolder);
		Flow3 flow3 = new Flow3(dataFolds);
		flow3.gen(sg);
	}
	public static void flow3run() throws Exception{
		//Run irlba
		System.out.println("flow3run");
		Data data = new Data(dataFolder, false, null);
		List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
		Flow3 flow3 = new Flow3(dataFolds);
		
		String cmd = "sh "+data.getScriptDir()+"flow3.sh &";
		flow3.run(cmd, data.getTempDir(), "flow3");
	}
	public static void flow3train() throws Exception{
		System.out.println("flow3train");
		Data data = new Data(dataFolder, false, null);
		List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
		ScriptGen sg = new ScriptGen(libFolder);
		Flow3 flow3 = new Flow3(dataFolds);
		
		flow3.collectGen(sg);
		
		String cmd = "sh "+data.getScriptDir()+"flow3train.sh &";
		flow3.run(cmd, data.getTempDir(), "flow3train");
	}
	
	
	public static void flow3end() throws Exception{
		//Collect Results from SVD-added classification
		System.out.println("flow3end");
		Data data = new Data(dataFolder, false, null);
		List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
		Flow3 flow3 = new Flow3(dataFolds);
		
		flow3.collectResult();
	}
	
	
	/* Clustering */
	
	/** Input from flow3run */
	public static void clusteringfolds(){
		try{
			System.out.println("clustering");
			Data data = new Data(dataFolder, false, null);
			List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
			ClusteringFolds cf = new ClusteringFolds(dataFolds);
			ScriptGen sg = new ScriptGen(libFolder);
			
			cf.gen(sg);
			
		}catch(Exception e){e.printStackTrace();}
	}
	public static void clusteringfoldsTrain(){
		try{
			System.out.println("clusteringTrain");
			Data data = new Data(dataFolder, false, null);
			List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
			ClusteringFolds cf = new ClusteringFolds(dataFolds);
			ScriptGen sg = new ScriptGen(libFolder);
			DbConn conn = new DbConn("127.0.0.1", "lbe_stream","root","sergeypk428571");
			
			cf.collectAndGen(sg, conn);
			
			conn.close();
		}catch(Exception e){e.printStackTrace();}
	}
	public static void clusteringfoldsCollect(){
		try{
			System.out.println("clusteringEnd");
			Data data = new Data(dataFolder, false, null);
			List<DataFold> dataFolds = DataFold.getDataFolds(data, false);
			ClusteringFolds cf = new ClusteringFolds(dataFolds);
			
			cf.collectResult();
		}catch(Exception e){e.printStackTrace();}
	}
}
